{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "de839a25-9533-45a9-b559-4421fc9db712",
   "metadata": {
    "tags": []
   },
   "source": [
    "# array数值的数值运算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "293e89da-4a84-4d7e-b63d-cdc7dc3dc635",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "fang_array = np.array([[1,2,3],[4,5,6]])\n",
    "fang_array"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "7d1e4fe9-9ba5-4815-b256-96091965a6ce",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "21"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(fang_array)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de69e76d-53fb-4c9b-afd1-ffab61c9580d",
   "metadata": {},
   "source": [
    "## 指定要进行的操作是沿着什么轴"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7c34c958-32bb-4d5d-a303-f6c18dc2cc25",
   "metadata": {},
   "source": [
    "### 计算累加"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "248ab24b-27f6-41cc-a3d1-77a2448221f2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([5, 7, 9])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(fang_array,axis=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "818107f6-9ee5-4ebf-b4b1-2e35d6a82f88",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 15])"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.sum(fang_array, axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "37d8a83e-ac5f-42ef-bee2-cac594af90e3",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 15])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#-1即最后一个轴\n",
    "np.sum(fang_array, axis=-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "e0004a0d-2af9-4423-a1d9-ec33c32b5239",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 6, 15])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.sum(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "84eed681-b569-48fd-b3bb-644934630a56",
   "metadata": {},
   "source": [
    "### 计算累乘"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "85bd08a9-53c8-44a5-94f0-48af9cdadb20",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "720"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.prod()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "886b1d86-f0eb-4e03-91db-cfc21ba217f8",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([  6, 120])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.prod(axis=1)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1897f9e1-e218-48bd-a078-126568cba252",
   "metadata": {},
   "source": [
    "### 最大最小值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "466473e1-e4b9-4008-babd-28b1cef59484",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.min()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "5afe4b18-b208-46a4-89a5-809ff385bd7a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1, 4])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.min(axis=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "f7310dff-645d-4813-ae47-f08afd040b32",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.max()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "9b0b3670-adee-4690-8d28-1c440fe527e2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([4, 5, 6])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.max(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1946c781-1e87-4c61-a2e4-d17838c1a8f4",
   "metadata": {
    "tags": []
   },
   "source": [
    "### 最大最小值的索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "193d5749-8bfe-4ad9-8616-1a23ab9d7037",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.argmin()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "8970d783-e812-4e59-95a3-7d537fea7cf2",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.argmax()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "62450b75-f2ae-407b-9b46-05d5782b461a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([0, 0, 0])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.argmin(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "451e9acf-5d2a-44e6-920f-619db0791888",
   "metadata": {},
   "source": [
    "### 均值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "304aa598-93fc-490b-863b-76dc0a2658f0",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "3.5"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "dc9f79c1-27e5-4053-885c-aa58e58c9a80",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([2.5, 3.5, 4.5])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.mean(axis=0)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1ec9e07c-5006-4096-9bf5-fed9e801f07e",
   "metadata": {},
   "source": [
    "### 标准差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "1a998602-6968-4775-94bc-2fc45205341a",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.707825127659933"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.std()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7dee9a0a-02a5-436b-b850-3b6fcdf3b1ec",
   "metadata": {},
   "source": [
    "### 方差"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "5840c3a2-cf91-42e6-b2b9-03254458d2dd",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.9166666666666665"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.var()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "de4a27ce-b4a8-46d4-864b-fcacd16fa948",
   "metadata": {},
   "source": [
    "## 其他计算"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "a9843510-b16f-421f-9cfd-405ef275a39d",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "643cbd9b-7ffa-405a-bfe6-f3c033221132",
   "metadata": {},
   "source": [
    "### 限制上下限"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "cd13c225-458e-4f14-aac1-e2b4a89d6f06",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[2, 2, 3],\n",
       "       [4, 4, 4]])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.clip(2,4)\n",
    "#小于2的变成2,大于4的变成4"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0c383295-03a3-47a7-b064-73da28c10d98",
   "metadata": {},
   "source": [
    "### 四舍五入"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "50b69ba2-33bc-4c2e-a367-b8380c83d324",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1., 4., 6.])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array = np.array([1.2, 3.6, 6.4])\n",
    "fang_array.round()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "a965a4d6-b30a-4a73-8367-26e51a471e8e",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.2, 3.6, 6.4])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.round(decimals=1) #四舍五入精度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "8dbb71df-f24f-434f-8e7c-7a269072e387",
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([1.2, 3.6, 6.4])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fang_array.round(1)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
